//
//  LoginViewController.m
//  SpeedDating
//
//  Created by mac on 2017/5/10.
//  Copyright © 2017年 isu. All rights reserved.
//

#import "LoginViewController.h"
#import "GifloginViewController.h"
#import "RegisterViewController.h"
#import "ForgetViewController.h"
#import "EVTPhoneAreaCodeViewController.h"
#import "MZCTabBarController.h"
#import "RegisterInfoViewController.h"
#import "RegisterPhotoViewController.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *phoneTextField;

@property (nonatomic, strong) UITextField *passwordTextField;

@property (nonatomic, strong) UIButton *loginButton;

@property (nonatomic, strong) UIView    *bgView;
@property (nonatomic, strong) UIView    *backView;
@property (nonatomic, strong) UIButton  *regionBtn;
@property (nonatomic, strong) NSString  *regionStr;
@property (nonatomic, strong) UILabel   *numberLab;
@property (nonatomic, strong) NSString  *numberStr;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgationBarHidden];
    [self configureUI];


}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}

- (void)configureUI {
    
    self.view.backgroundColor =Colorbackground;
    self.phoneTextField.text = [HQHttpManager userAccount];
    self.passwordTextField.text = [HQHttpManager userPassword];
    self.passwordTextField.secureTextEntry = YES;
        _regionStr = @"中国";
        _numberStr = @"86";
    //返回View
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 64)];
    self.backView.backgroundColor = Colorbackground;
    [self.view addSubview:self.backView];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(22, 19.5, 44, 44)];
    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置，现设为居左，默认的是居中
    
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 5.5, 0, 0);
    [btn addTarget:self action:@selector(backActionbtn) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 16, KScreenWidth, 44*SCREEN_WIDTH_RATIO47)];
    titleLab.text = @"手机号登录";
    titleLab.font = [UIFont systemFontOfSize:18];
    titleLab.textColor = ColorI(0x2e353f);
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self.backView addSubview:titleLab];
    [self.backView addSubview:btn];

    //背景View
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.bgView];

    
    
    UILabel *regionLab = [[UILabel alloc] init];
    regionLab.text = @"国家和地区";
    regionLab.font = FontBold(16);
    regionLab.textAlignment = NSTextAlignmentLeft;
    regionLab.textColor = [UIColor blackColor];
    [self.bgView addSubview:regionLab];
    [regionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(19*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(self.bgView).offset(0);
        make.height.mas_equalTo(58*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(KScreenWidth/2-19*SCREEN_WIDTH_RATIO47);
    }];
  
    
    _regionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_regionBtn setTitle:_regionStr forState:UIControlStateNormal];
    _regionBtn.titleLabel.font = FontBold(16);
    _regionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_regionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_regionBtn addTarget:self action:@selector(regionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:_regionBtn];

    [_regionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(regionLab.mas_right).offset(0);
        make.top.equalTo(self.bgView).offset(0);
        make.height.mas_equalTo(55*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(KScreenWidth/2-33*SCREEN_WIDTH_RATIO47);
    }];
    
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"jiantou2"];
    [self.bgView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_regionBtn.mas_right).offset(0);
        make.top.equalTo(self.bgView).offset(21*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(14);
        make.width.mas_equalTo(14);
    }];

    UIView *linView = [[UIView alloc] init];
    linView.backgroundColor = RGB(242, 244, 244);
    [self.bgView addSubview:linView];
    [linView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView).offset(0);
        make.top.equalTo(regionLab.mas_bottom).offset(1);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(KScreenWidth);
    }];


    _numberLab = [[UILabel alloc]init];
    NSMutableAttributedString *messageStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"+ %@",_numberStr] attributes:nil];
    [messageStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,messageStr.length)];
    [messageStr addAttribute:NSFontAttributeName value:FontBold(13) range:NSMakeRange(0,1)];
    [messageStr addAttribute:NSFontAttributeName value:FontBold(16) range:NSMakeRange(1, messageStr.length-1)];
    self.numberLab.attributedText = messageStr;

    self.numberLab.textAlignment = NSTextAlignmentCenter;
    [self.bgView addSubview:self.numberLab];
    [_numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(13);
        make.top.equalTo(linView).offset(1);
        make.height.mas_equalTo(55*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(55*SCREEN_WIDTH_RATIO47);
    }];

    _phoneTextField = [[UITextField alloc] init];
    _phoneTextField.placeholder = @"请输入手机号";
    _phoneTextField.font = FontP(15);
    _phoneTextField.textColor = ColorI(0x802e353f);
    _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.bgView addSubview:_phoneTextField];
    [_phoneTextField addTarget:self action:@selector(valueChange) forControlEvents:UIControlEventEditingChanged];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_numberLab.mas_right).offset(10);
        make.top.equalTo(linView).offset(1);
        make.height.mas_equalTo(55*SCREEN_WIDTH_RATIO47);
        make.right.mas_equalTo(-30*SCREEN_WIDTH_RATIO47);

    }];
    UIView *linView2 = [[UIView alloc] init];
    linView2.backgroundColor =RGB(242, 244, 244);
    [self.bgView addSubview:linView2];
    [linView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(0);
        make.top.equalTo(_phoneTextField.mas_bottom).offset(1);
        make.height.mas_equalTo(1);
        make.right.mas_equalTo(0);

    }];
    
    UILabel *passwordLab = [[UILabel alloc] init];
    passwordLab.text = @"密码";
    passwordLab.font = FontBold(16);
    passwordLab.textAlignment = NSTextAlignmentCenter;
    passwordLab.textColor = [UIColor blackColor];
    [self.bgView addSubview:passwordLab];
    [passwordLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(13);
        make.top.equalTo(linView2.mas_bottom).offset(1);
        make.height.mas_equalTo(55*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(55*SCREEN_WIDTH_RATIO47);
    }];

    _passwordTextField = [[UITextField alloc] init];
    _passwordTextField.placeholder = @"请输入密码";
    _passwordTextField.font = Font(15);
    self.passwordTextField.secureTextEntry = YES;
    _passwordTextField.textColor = ColorI(0x802e353f);
    [self.bgView addSubview:_passwordTextField];
    [_passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordLab.mas_right).offset(10);
        make.top.equalTo(linView2.mas_bottom).offset(1);
        make.height.mas_equalTo(55*SCREEN_WIDTH_RATIO47);
        make.right.mas_equalTo(-30*SCREEN_WIDTH_RATIO47);

    }];
    
    UIView *linView3 = [[UIView alloc] init];
    linView3.backgroundColor =RGB(242, 244, 244);

    [self.bgView addSubview:linView3];
    [linView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(0);
        make.top.equalTo(_passwordTextField.mas_bottom).offset(1);
        make.height.mas_equalTo(1);
        make.right.mas_equalTo(0);
    }];
    
    _loginButton = [[UIButton alloc] init];
    _loginButton.backgroundColor = Colorbackground;

//    _loginButton.layer.cornerRadius = 25;
//    _loginButton.layer.shadowColor = RGB(254, 240, 178).CGColor;
//    _loginButton.layer.shadowOffset = CGSizeMake(8.0f, 8.0f);
//    _loginButton.layer.shadowOpacity = 0.6f;
//    _loginButton.layer.shadowRadius=5;
    _loginButton.layer.cornerRadius = 9;
    
    _loginButton.layer.masksToBounds = YES;
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.bgView addSubview:_loginButton];
    [_loginButton addTarget:self action:@selector(clickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(35*SCREEN_WIDTH_RATIO47);
        make.right.equalTo(self.bgView).offset (-35*SCREEN_WIDTH_RATIO47);
        make.top.equalTo(linView3.mas_bottom).offset(150*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
    }];
    UIButton *registerButton = [[UIButton alloc] init];
    [registerButton setTitle:@"手机号码注册" forState:UIControlStateNormal];
    [registerButton setTitleColor:ColorI(0xe6be00) forState:UIControlStateNormal];
    registerButton.titleLabel.font = FontP(16);
    [self.bgView addSubview:registerButton];
    [registerButton addTarget:self action:@selector(clickRegisterButton) forControlEvents:UIControlEventTouchUpInside];
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_loginButton);
        make.top.equalTo(_passwordTextField.mas_bottom).offset(10*SCREEN_WIDTH_RATIO47);
    }];
    UIButton *forgetButton = [[UIButton alloc] init];
    [forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetButton setTitleColor:ColorI(0xe6be00) forState:UIControlStateNormal];
    forgetButton.titleLabel.font = FontP(16);
    [self.bgView addSubview:forgetButton];
    [forgetButton addTarget:self action:@selector(clickForgetButton) forControlEvents:UIControlEventTouchUpInside];
    [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_loginButton).offset(18);
        make.top.equalTo(_passwordTextField.mas_bottom).offset(10);
    }];
//    forget_icon
//    phone_icon
    
    UIImageView *forgetImg = [[UIImageView alloc] init];
    forgetImg.image = [UIImage imageNamed:@"forget_icon"];
//    forgetImg.backgroundColor = ColorI(0xe6be00);
    [self.bgView addSubview:forgetImg];
    [forgetImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(forgetButton.mas_left).offset(-5);
        make.top.equalTo(_passwordTextField.mas_bottom).offset(18);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(20);
    }];
    UIImageView *phoneImg = [[UIImageView alloc] init];
    phoneImg.image = [UIImage imageNamed:@"ipone"];
//    phoneImg.backgroundColor = ColorI(0xe6be00);
    [self.bgView addSubview:phoneImg];
    [phoneImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(registerButton.mas_left).offset(-5);
        make.top.equalTo(_passwordTextField.mas_bottom).offset(18);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(15);
    }];
    
}

- (void)clickLoginButtonphone{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请输入正确手机号" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
    }];
    
    [alertVC addAction:confirAction];
    [self.navigationController presentViewController:alertVC animated:YES completion:nil];

}
- (void)clickLoginButtonpasswordTextField{

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请输入六位以上密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:confirAction];
        [self.navigationController presentViewController:alertVC animated:YES completion:nil];
    

}

- (void)clickLoginButton {
    [self.bgView endEditing:YES];
    _loginButton.enabled = NO;
    if (_phoneTextField.text.length == 0||_phoneTextField.text.length != 11) {
        [self clickLoginButtonphone];
        _loginButton.enabled = YES;
        return ;
    }
    if (_passwordTextField.text.length < 6) {
        [self clickLoginButtonpasswordTextField];
        _loginButton.enabled = YES;
        return ;
    }
    [SVProgressHUD show];
    NSString* identifierNumber = [[UIDevice currentDevice].identifierForVendor UUIDString] ;
    NSLog(@"手机序列号: %@",identifierNumber);
    __weak __typeof(self) blockSelf = self;
    [HQHttpManager userLoginWithAccout:_phoneTextField.text password:_passwordTextField.text thirdNO:nil thirdType:nil mode:@"mobile" nickName:nil deviceNo:identifierNumber successBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
        [SVProgressHUD dismiss];
        if (isSuccess) {
            
            //登录腾讯云
                _loginButton.enabled = YES;
                [blockSelf loginSuccess];
            
//            if ([strinfoStatus isEqualToString:@"ok"]) {
//                [blockSelf loginSuccess];
//            }else if([strinfoStatus isEqualToString:@"baseinfo"]){
//                RegisterInfoViewController *infoVC = [[RegisterInfoViewController alloc] init];
//                [self.navigationController pushViewController:infoVC animated:YES];
//            }else if ([strinfoStatus isEqualToString:@"photo"]){
//                RegisterPhotoViewController *photoVC = [[RegisterPhotoViewController alloc] init];
//                photoVC.sexNum = [HQHttpManager getUserSex];
//                [self.navigationController pushViewController:photoVC animated:YES];
//
//            }
        } else {
            _loginButton.enabled = YES;
        }
    }];
}

- (void)loginSuccess {
    [self.bgView endEditing:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_isLogin object:@(NO)];

    
}

- (void)clickRegisterButton {
    RegisterViewController *RVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:RVC animated:YES];
    
}
//忘记密码
- (void)clickForgetButton {
    ForgetViewController *RVC = [[ForgetViewController alloc] init];
    [self.navigationController pushViewController:RVC animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.bgView endEditing:YES];
    
}

- (void)valueChange {
    if (_phoneTextField.text.length>11) {
        _phoneTextField.text = [_phoneTextField.text substringToIndex:11];
        [self clickLoginButtonphone];
    }
}
#pragma mark ===================选择城市=====================
- (void)regionBtnClick:(UIButton *)sender{
    EVTPhoneAreaCodeViewController *vc = [[EVTPhoneAreaCodeViewController alloc]init];
    vc.completion = ^(NSString *name, NSString *code){
        
        UIButton *b = sender;
        [b setTitle:[NSString stringWithFormat:@"%@",name] forState:UIControlStateNormal];
        _regionStr = name;
        _numberStr = code;
        NSMutableAttributedString *messageStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"+ %@",_numberStr] attributes:nil];
        [messageStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,messageStr.length)];
        [messageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0,1)];
        [messageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(1, messageStr.length-1)];
        self.numberLab.attributedText = messageStr;
    };
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)backActionbtn{
    _backView.hidden = YES;
    [self setNavgationBarHiddenNo];

    [self.navigationController popViewControllerAnimated:YES];
}
@end
